package com.example.springboot_redis.contoller;

import com.example.springboot_redis.domain.User;
import com.example.springboot_redis.kakfa.KafkaProducer;
import com.example.springboot_redis.redis.DistributedRedisLock;
import org.redisson.api.RList;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.IntegerCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

/**
 * 测试Redis的controller
 *
 * @author qp
 * @date 2019/7/19 9:50
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private DistributedRedisLock distributedRedisLock;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private KafkaProducer kafkaProducer;


    // 获取字符串
    @GetMapping("/get/{key}")
    public String getRedis(@PathVariable(name = "key") String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }


    @GetMapping("/kafka")
    public String getKafka() {
        kafkaProducer.send("aaaaaaaaaaaaa");
        return "SUCCESS";
    }

    @GetMapping("/kill")
    public String kill() throws InterruptedException {
        distributedRedisLock.lock("lock");

        RMapCache<String, Integer> mapCache = redissonClient.getMapCache("nameSpace", IntegerCodec.INSTANCE);

      //  mapCache.putIfAbsent("redisKey", 0, 1, TimeUnit.DAYS);

        int incr = mapCache.addAndGet("redisKey", 1);//加1并获取计算后的值

        String requestkey = UUID.randomUUID().toString();
        RList<String> list = redissonClient.getList("keylock12");
        if(list.size() < 300){
            list.add(requestkey);
            kafkaProducer.send(requestkey);
            System.out.println("秒杀人数: "+ list.size());
            return  "SUCEESS";
        }
        System.out.println("秒杀失败");

        System.out.println("秒杀次数： "+incr);
        distributedRedisLock.unlock("lock");

        return "秒杀失败";
    }


    @GetMapping("/test")
    public String getRedis() throws InterruptedException {
        String requestkey = UUID.randomUUID().toString();
       //distributedRedisLock.lock("LOCK");

        System.out.println(requestkey+" start");
        Thread.sleep(new Random(2500).nextInt(100)*1000);
        System.out.println(requestkey+" end  ");
        //distributedRedisLock.unlock("LOCK");
        return "SUCCESS";
    }

    // 保存字符串
    @GetMapping("/set/{key}/{value}")
    public String getRedis(@PathVariable(name = "key") String key, @PathVariable(name = "value") String value) {
        stringRedisTemplate.opsForValue().set(key, value);
        return "SUCCESS";
    }

    @GetMapping("/sets")
    public String getSetRedis() {
        SetOperations<String, String> set = redisTemplate.opsForSet();
        set.add("set1","22");
        set.add("set1","33");
        set.add("set1","44");
        set.add("set1","44");
        Set<String> resultSet =redisTemplate.opsForSet().members("set1");
        System.out.println("resultSet:"+resultSet);
        return "SUCCESS";
    }

    // 保存对象
    @PostMapping("/postEntity")
    public String postEntity(@RequestBody User user) {
       // redisTemplate.opsForValue().set(user.getUserCode(), user);
        return "SUCCESS";
    }

    // 获取对象
    @GetMapping("/getEntity/{key}")
    public User getEntity(@PathVariable(name = "key") String key) {
        return (User) redisTemplate.opsForValue().get(key);
    }

}
